//package com.seaside.park.service;
//
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.util.EntityUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.nio.file.Files;
//import java.nio.file.Paths;
//import java.security.KeyFactory;
//import java.security.NoSuchAlgorithmException;
//import java.security.PrivateKey;
//import java.security.spec.InvalidKeySpecException;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.util.Base64;
//
//@Slf4j
//@Service
//public class WxPayH5Service {
//
//    @Value("${weixin.mchId}")
//    private String mchId;
//    @Value("${weixin.mchSerialNo}")
//    private String mchSerialNo;
//    private String privateKey;
//    @Value("${weixin.apiV3Key}")
//    private String apiV3Key;
//
//    public void CreateOrder() throws IOException {
//        //请求URL
//        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/h5");
//        HttpClient httpClient;
//
//        // 请求body参数
//        String reqdata = "{"
//                + "\"amount\": {"
//                + "\"total\": 100,"
//                + "\"currency\": \"CNY\""
//                + "},"
//                + "\"scene_info\": {"
//                + "\"payer_client_ip\":\"14.23.150.211\","
//                + "\"h5_info\": {"
//                + "\"type\": \"IOS\"" + "}},"
//                + "\"mchid\": \"1900006891\","
//                + "\"description\": \"Image形象店-深圳腾大-QQ公仔\","
//                + "\"notify_url\": \"https://www.weixin.qq.com/wxpay/pay.php\","
//                + "\"out_trade_no\": \"1217752501201407033233388881\","
//                + "\"goods_tag\": \"WXG\","
//                + "\"appid\": \"wxdace645e0bc2c424\"" + "}";
//        StringEntity entity = new StringEntity(reqdata, "utf-8");
//        entity.setContentType("application/json");
//        httpPost.setEntity(entity);
//        httpPost.setHeader("Accept", "application/json");
//
//        privateKey = String.valueOf(getPrivateKey("C:\\Users\\Administrator\\Desktop\\WXCertUtil\\cert\\apiclient_key.pem"));
//        // 加载商户私钥（privateKey：私钥字符串）
//        PrivateKey merchantPrivateKey = PemUtil
//                .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
//
//        // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
//        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
//                new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),apiV3Key.getBytes("utf-8"));
//
//        // 初始化httpClient
//        httpClient = WechatPayHttpClientBuilder.create()
//                .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
//                .withValidator(new WechatPay2Validator(verifier)).build();
//
//        try {
//            CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost);
//            int statusCode = response.getStatusLine().getStatusCode();
//            if (statusCode == 200) {
//                System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
//            } else if (statusCode == 204) {
//                System.out.println("success");
//            } else {
//                System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity()));
//            }
//        } catch (ClientProtocolException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static PrivateKey getPrivateKey(String filename) throws IOException {
//
//        String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
//        try {
//            String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
//                    .replace("-----END PRIVATE KEY-----", "")
//                    .replaceAll("\\s+", "");
//
//            KeyFactory kf = KeyFactory.getInstance("RSA");
//            return kf.generatePrivate(
//                    new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
//        } catch (NoSuchAlgorithmException e) {
//            throw new RuntimeException("当前Java环境不支持RSA", e);
//        } catch (InvalidKeySpecException e) {
//            throw new RuntimeException("无效的密钥格式");
//        }
//    }
//}
